<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/18
 * Time: 下午6:09
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Looting extends Base
{

    private $redis_looting_join_num = 'LOOTING:JOIN:NUM';
    private $redis_looting_info = 'LOOTING:INFO';
    private $redis_looting_gifts = 'LOOTING:GIFTS';
    private $redis_looting_switch = 'LOOTING:SWITCH';

    protected $tableLooting = 'looting';
    protected $tableLootingDispatch = 'looting_dispatch';
    protected $tableLootingGift = 'looting_gift';


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取夺宝屋状态
     * @param $room_id
     * @return mixed
     */
    public function getStatus($room_id){
        $data = DB::table($this->tableLooting)
            ->where(['room_id' => $room_id, 'status' => 0]) //0=>未完成 ，1=》已完成
            ->first();
        return self::objToArray($data);
    }

    /**
     * 获取夺宝屋信息
     * @param $looting_id
     * @return mixed
     */
    public function getLootingInfo($looting_id){
        $looting = Redis::HGET($this->redis_looting_info, $looting_id);
        if(empty($looting) || $looting == "null"){
            $data = DB::table($this->tableLooting)->find($looting_id);
            $data = self::objToArray($data);
            if(!empty($data)){
                Redis::HSET($this->redis_looting_info, $looting_id, json_encode($data));
            }
            return $data;
        }
        return json_decode($looting ,true);
    }

    public function getDispatchNum($looting_id ,$user_id){
        return DB::table($this->tableLootingDispatch)
            ->where('looting_id' , $looting_id)
            ->where('user_id' ,$user_id)
            ->count();
    }
    /**
     * 获取夺宝屋的队员
     * @param $looting_id
     * @return array
     */
    public function getJoinUser($looting_id){
        $users = DB::table($this->tableLootingDispatch)
            ->where('looting_id' , $looting_id)
            ->orderBy('id','asc')
            ->get();
        $users =  self::objToArray($users);
        foreach ($users as $user){
            if(isset($u[$user['user_id']])){
                $u[$user['user_id']]['join_time'] = date('m-d H:i' ,strtotime($user['join_time']));
                $times = $u[$user['user_id']]['num']+1;
                $u[$user['user_id']]['num'] = $times;
            }else{
                $u[$user['user_id']]['join_time'] = date('m-d H:i' ,strtotime($user['join_time']));;
                $u[$user['user_id']]['num'] = 1;
            }
        }
        return isset($u) ? $u : [];
    }

    /**
     * 获取夺宝屋可选择礼物
     * @return mixed
     */
    public function getGifts(){
        $looting_gifts = Redis::get($this->redis_looting_gifts);
        if(empty($looting_gifts)){
            $data = DB::table($this->tableLootingGift)->select('gift_id')->get();
            $data = self::objToArray($data);
            if(!empty($data)){
                Redis::set($this->redis_looting_gifts ,json_encode($data));
            }
            return $data;
        }
        return json_decode($looting_gifts ,true);
    }

    /**
     * 获取已经完成的夺宝屋
     * @param $room_id
     * @return mixed
     */
    public function getFinishLooting($room_id){
        $looting = DB::table($this->tableLooting)
            ->where(['room_id'=>$room_id ,'status'=>1])
            ->orderBy('winner_time' ,'desc')
            ->get();
        return self::objToArray($looting);
    }

    /**
     * @param $params
     */
    public function insertLooting($params =[]){
        return DB::table($this->tableLooting)->insertGetId($params);
    }

    /**
     * @param $looting_id
     * @param $params
     * @return int
     */
    public function updateLooting($looting_id , $params){
        $ret = DB::table($this->tableLooting)
            ->where(['id' => $looting_id])
            ->update($params);
        if($ret){
            Redis::HDEL($this->redis_looting_info, $looting_id);
        }
        return $ret;
    }

    /**
     * 加入夺宝屋
     * @param array $params
     * @return int
     */
    public function insertLootingUser($params =[]){
        $params['join_time'] = date('Y-m-d H:i:s');
        return DB::table($this->tableLootingDispatch)->insertGetId($params);
    }

    /**
     * 获取夺宝屋已加入人数
     * @param $looting_id
     * @return int
     */
    public function getLootingJoinNum($looting_id){
        $join_num = Redis::HGET($this->redis_looting_join_num, $looting_id);
        if(empty($looting)){
            $userNum = DB::table($this->tableLootingDispatch)
                ->where('looting_id' , $looting_id)
                ->count();
            $join_num = empty($userNum) ? 0 : $userNum;
            Redis::HSET($this->redis_looting_join_num, $looting_id, $join_num);
            return $join_num;
        }
        return $join_num;
    }

    /**
     * 更新夺宝屋加入人数
     * @param $looting_id
     */
    public function setLootingJoinNum($looting_id){
        $userNum = DB::table($this->tableLootingDispatch)
            ->where('looting_id' , $looting_id)
            ->count();
        $join_num = empty($userNum) ? 0 : $userNum;
        Redis::HSET($this->redis_looting_join_num, $looting_id, $join_num);
    }

    /**
     * 获取开关
     */
    public function getSwitch(){
        $switch = Redis::get($this->redis_looting_switch);
        if(empty($switch) || $switch ==0){
            return 0;
        }
        return 1;
    }

    /**
     * 设置开关
     * @param $switch 0=>关闭 1=>开启
     * @return mixed
     */
    public function setSwitch($switch){
        Redis::set($this->redis_looting_switch ,$switch);
        return Redis::get($this->redis_looting_switch);
    }

    public function CUDLootingGift($data = [], $type = 'insert', $id = 0)
    {
        switch ($type) {
            case 'insert':
                $data['created_at'] = date('Y-m-d H:i:s');
                $ret = DB::table($this->tableLootingGift)->insertGetId($data);
                break;
            case 'update':
                $data['updated_at'] = date('Y-m-d H:i:s');
                $ret = DB::table($this->tableLootingGift)->where(['id' => $id])->update($data);
                break;
            case 'delete':
                $ret = DB::table($this->tableLootingGift)->where(['id' => $id])->delete();
                break;
            default:
                $ret = false;
                break;
        }
        Redis::DEL($this->redis_looting_gifts);
        return $ret;
    }

    public function getJoinUserIds($looting_id){
        $users = DB::table($this->tableLootingDispatch)
            ->where('looting_id' , $looting_id)
            ->orderBy('id','asc')
            ->get();
        return self::objToArray($users);
    }

}